// pages/square/square/square.js
import Article from "../../../manager/Article";
import {
  toggleTime
} from "../../../util/updatetimeshow"



const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: app.text.square.square,
    headerHeight: app.globalData.navHeight + 20,
    userInfo: '',
    page: 0,
    lists: [],
    isLike: false,
    noMore: false,

    myVisual: false, // 当前视角 true是我，false是广场
  },

  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1,
        page: this
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.nav = this.selectComponent("#nav");
    this.toast = this.selectComponent("#toast");

    this.loadData();
  },

  liked: function (e) {
    let item = e.currentTarget.dataset.item;
    // console.log(item)
    let likes = item.poster_likes ? item.poster_likes : []

    // 在lists 数组中找到操作项数据
    let lists = this.data.lists
    const isLargeNumber = (element) => element._id == item._id;
    let item_index = lists.findIndex(isLargeNumber)
    // console.log(lists[item_index])


    this.toast.showLoading();

    likes.push(this.data.userInfo._openid)

    if (!item.isLike) {

      lists[item_index].poster_likes = likes
      lists[item_index].isLike = true

      this.toast.showSuccess("点赞成功");

      this.setData({
        lists: lists,
      });

      // 点赞添加请求
      Article.update(item._id, {
        poster_likes: likes,
      }).then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err)
        this.toast.showFailure(err);
      });

    } else {
      // 
      this.toast.showWarning("已经点过赞了");
    }





  },

  /**
   * 加载数据
   */
  loadData: function () {
    let userInfo = app.globalData.userInfo;
    let partnerInfo = app.globalData.partnerInfo;

    this.toast.showLoading();
    Article.lists(this.data.page, (this.data.myVisual ? userInfo._openid : null), (partnerInfo == null ? null : partnerInfo._openid)).then(res => {
      this.toast.hideLoading();
      wx.stopPullDownRefresh();

      let lists = (this.data.page == 0 ? [] : this.data.lists);

      res.forEach((item, _) => {
        let likes = item.poster_likes ? item.poster_likes : []
        // console.log(item.poster_likes)
        // console.log(userInfo._openid)
        item.isLike = false
        if (likes.includes(userInfo._openid)) {
          item.isLike = true
        }

        item.formatTime = toggleTime(item.time);
        let time = app.util.formatTime(item.time, false, false);
        time = time.split("-");
        // item.formatTime = time[1] + "-" + time[2];
        item.formatTimeFull = toggleTime(item.time);
        if (item.partner_comment) {
          item.partner_comment.forEach(e => e.partner_time = toggleTime(e.partner_time));
          // console.log(item.partner_comment)
        }
        lists.push(item);
      });

      this.setData({
        page: this.data.page + 1,
        lists: lists,
        userInfo: userInfo,
        noMore: (res.length == 0)
      });

      // console.log(this.data.lists)

    }).catch(err => {
      this.toast.showFailure(err);
    })
  },

  /**
   * 切换视角
   */
  changeVisual: function (e) {
    let visual = e.currentTarget.dataset.type;
    let my = (visual == "my");

    if (this.data.myVisual == my) {
      return;
    }

    this.setData({
      myVisual: my,
      lists: [],
      page: 0,
      noMore: false
    });

    this.loadData();
  },

  /**
   * 详情
   */
  detail: function (e) {
    let item = JSON.stringify(e.currentTarget.dataset.item);
    // console.log(item)
    wx.navigateTo({
      url: '/pages/square/detail/detail?item=' + item
    });
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      noMore: false,
    });

    this.loadData();
  },

  /**
   * 上滑加载更多
   */
  onReachBottom: function () {
    this.loadData();
  },

  onPageScroll: function (e) {
    if (e.scrollTop <= app.globalData.headerHeight) {
      this.nav.hide();
    } else {
      this.nav.show();
    }
  },

})